package com.untour.seqid.config.mybatis;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


public class MyLambdaUpdateWrapper<T> extends LambdaUpdateWrapper<T> {

    public MyLambdaUpdateWrapper() {
        super();
    }

    public MyLambdaUpdateWrapper(Class<T> entityClass) {
        super(entityClass);
    }
    public MyLambdaUpdateWrapper<T> setIncrBy(SFunction<T, ?>...columns) {
        return this.setIncrBy(true,columns);
    }

    public MyLambdaUpdateWrapper<T> setIncrBy(boolean condition,SFunction<T, ?>...columns) {
        return this.setBy(condition, columns,Constants.PLUS);
    }

    public MyLambdaUpdateWrapper<T> settDecrBy(SFunction<T, ?>...columns) {
        return this.settDecrBy(true,columns);
    }
    public MyLambdaUpdateWrapper<T> settDecrBy(boolean condition,SFunction<T, ?>...columns) {
        return this.setBy(condition, columns,Constants.DASH);
    }

    private MyLambdaUpdateWrapper<T> setBy(boolean condition, SFunction<T, ?>[] columns,String symbol) {
        if(condition){
            if(columns.length < 2){
                throw new MybatisPlusException("参数错误");
            }
            List<String> realColumns = new ArrayList<>(columns.length);
            for (SFunction<T, ?> column : columns) {
                realColumns.add(super.columnToString(column));
            }
            super.setSql(realColumns.get(0) + Constants.EQUALS
                    + realColumns.stream().collect(Collectors.joining(symbol)));
        }
        return this;
    }
}
